package com.candy.candycommon.Event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;

import java.net.UnknownHostException;

/**
 * @author suYanLong
 * @date 2022年10月14日 14:27
 */
@Slf4j
public class ExceptionEventListener {

    private static ExceptionCallBack callBack;

    public ExceptionEventListener() {
    }

    public ExceptionEventListener(ExceptionCallBack callBack) {
        this.callBack = callBack;
    }

    @EventListener
    public void scoreEvent(ExceptionEvent exceptionEvent) throws UnknownHostException {
        if (exceptionEvent instanceof ExceptionEvent.SendExceptionEvent) {
            if (exceptionEvent.getData() instanceof Exception) {
                log.error("异常事件", exceptionEvent);
            }
        }
        if (callBack != null)
            callBack.callBack(exceptionEvent);
    }

    @FunctionalInterface
    public interface ExceptionCallBack<S> {

        void callBack(S s) throws UnknownHostException;

    }

}
